﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IP2Client
{
    /// <summary>
    /// 
    /// </summary>
    public partial class PatientHistoryFile : Form
    {
        private List<string> patientSessionList = new List<string>();
        private string selectedPatient;
        private string selectedFile;

        /// <summary>
        /// Constructor for PatienHistoryFile
        /// </summary>
        /// <param name="selectedPatient">The selected patient</param>
        public PatientHistoryFile(string selectedPatient)
        {
            InitializeComponent();

            this.selectedPatient = selectedPatient;

            getHistorySelectedPatient();

            handleTcpClientUpdates();
        }

        /// <summary>
        /// Form is exitted
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Asks the server to send all data regarding the selectedPatient
        /// </summary>
        public void getHistorySelectedPatient()
        {
            ConnectionHandler.SendObject("!sndSessionList" + " " + selectedPatient);
        }

        /// <summary>
        /// Handles all the data send from the server to this application
        /// </summary>
        public void handleTcpClientUpdates()
        {
            object rcvdObject = null;

            if (ConnectionHandler.HandledClient == null)
            {
                return;
            }

            while (rcvdObject == null)
            {
                if (ConnectionHandler.HandledClient.Available > 0)
                {
                    rcvdObject = ConnectionHandler.ReadObject();

                    patientSessionList = rcvdObject as List<String>;

                    if (patientSessionList != null && patientSessionList.Count != 0)
                    {
                        updatePatientHistoryListView();
                    }
                }
            }
        }

        /// <summary>
        /// Adds all the data in the patientSessionList to the patient historyList. 
        /// Now it can be viewed
        /// </summary>
        public void updatePatientHistoryListView()
        {
            if(patientSessionList != null && patientSessionList.Count != 0)
            {
                foreach (string file in patientSessionList)
                {
                    patientHistoryListView.Items.Add(file);
                }
            }
        }

        /// <summary>
        /// Evnet that handles index changes in the patientHistoryList
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void patientHistoryListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListView.SelectedIndexCollection indexes = patientHistoryListView.SelectedIndices;

            foreach (int index in indexes)
            {
                selectedFile = patientHistoryListView.Items[index].SubItems[0].Text;
            }

            //For each ListViewItem in users connected to server list
            foreach (ListViewItem item in patientHistoryListView.Items)
            {
                //If ListViewItem is selected user, mark user in connetec to server list
                if (item.Text == selectedFile)
                {
                    item.BackColor = Color.LightSteelBlue;
                }
                else
                {
                    item.BackColor = Color.White;
                }
            }   
        }

        /// <summary>
        /// retrieves the history associated with the selected file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void getHistoryButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (selectedFile == string.Empty)
                {
                    MessageBox.Show("Select a session file from the list"); return;
                }

                string[] splitSelectedFile = selectedFile.Split(null);

                string date = splitSelectedFile[0];
                string file = splitSelectedFile[2];

                this.Hide();
                this.Dispose();

                Form patientHistoryChart = new PatientHistoryChart(selectedPatient, date, file);
                patientHistoryChart.ShowDialog();
            }
            catch (Exception){}
        }   
    }
}
